package handler

import (
	"ecomm-pro/cache"
	"ecomm-pro/models"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func ShopOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var shop models.ShopOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("shops").Select(
			"shop_orders.id AS OrderId",
			"shop_orders.order_num AS OrderNum",
			"shop_orders.order_price AS OrderPrice",
			"shops.shop_name AS ShopName",
			"shops.shop_price AS ShopPrice",
			"shops.shop_status AS ShopStatus",
			"shops.shop_type AS ShopType",
		).Joins("LEFT JOIN shop_orders ON shops.id=shop_orders.id").
			Where("shop_orders.id=?", id).Find(&shop)
		jsonData, _ := json.Marshal(shop)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shop)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": shop,
		"msg":  "ok",
	})
	return
}
func ShopOrderList(c *gin.Context) {
	//SELECT
	//shop_orders.id AS OrderId,
	//shop_orders.order_num AS OrderNum,
	//shop_orders.order_price AS OrderPrice,
	//shops.shop_name AS ShopName,
	//shops.shop_price AS ShopPrice,
	//shops.shop_status AS ShopStatus,
	//shops.shop_type AS ShopType
	//FROM
	//shops
	//LEFT JOIN shop_orders ON shops.id=shop_orders.id
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var shop []models.ShopOrderList
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("shops").Select(
			"shop_orders.id AS OrderId",
			"shop_orders.order_num AS OrderNum",
			"shop_orders.order_price AS OrderPrice",
			"shops.shop_name AS ShopName",
			"shops.shop_price AS ShopPrice",
			"shops.shop_status AS ShopStatus",
			"shops.shop_type AS ShopType",
		).Joins("LEFT JOIN shop_orders ON shops.id=shop_orders.id").Offset(offset).Limit(size).Find(&shop)
		jsonData, _ := json.Marshal(shop)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shop)
	}
	var total int64
	models.DB.Model(&models.ShopOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  shop,
		"msg":   "ok",
		"total": total,
	})
	return
}
func ShopOrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_price := c.PostForm("order_price")
	order_num := c.PostForm("order_num")
	data := models.ShopOrder{
		Model:      gorm.Model{},
		OrderNo:    order_no,
		OrderPrice: order_price,
		OrderNum:   order_num,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ShopAdd(c *gin.Context) {
	shop_name := c.PostForm("shop_name")
	shop_type := c.PostForm("shop_type")
	shop_price := c.PostForm("shop_price")
	shop_status := c.PostForm("shop_status")
	data := models.Shop{
		Model:      gorm.Model{},
		ShopName:   shop_name,
		ShopType:   shop_type,
		ShopPrice:  shop_price,
		ShopStatus: shop_status,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
